<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :disabled="mode=='show'" :rules="rules" ref="dialogForm" label-width="100px">
			<el-form-item label="重置密码：" prop="password">
					<el-input type="password" v-model="form.password" show-password></el-input>
				</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '重置密码',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					userId:"",
					password: ''
				},
				//所需数据选项
				groups: [],
				rules: {
					password: [
						{ required: true, message: '请输入密码'},
						{min: 6, message: '长度需为6位'}
					],
				}
			}
		},

		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.auth.resetUserPwd.put(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.userId = data.userId
			}
		}
	}
</script>

<style>
</style>
